/************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     rtc.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-01
  * @brief    
 *************************************************************************/

#include "rtc.h"
//#include "rtc_drv.h"
//#include "cpm_reg.h"
//#include "sys.h"
//zyy----------------------------
#include "gif.h"
#include "analogClock.h"
#include "digitalClock.h"
#include "scrollText.h"
#include "uiCounter.h"
#include "varCounter.h"
#include "needle.h"
#include "gesture.h"
#include "button.h"
#include "varButton.h"
#include "sliderBar.h"
#include "circularTouch.h"
#include "slideMenu.h"
#include "keyBoard.h"
#include "multiVarButton.h"
#include "uiCounter.h"
#include "popupBox.h"
#include "curve.h"
#include "uiQRCode.h"
#include "uiEncoder.h"
#include "uiIcon.h"
//--------------------------------


extern uint32_t SYS_CLOCK;



_calendar_obj calendar; // Clock structure


uint8_t LT_AsciiToBcd(uint8_t asciiData)
{
	uint8_t bcdData = 0;
	bcdData = (((asciiData / 10) << 4) | ((asciiData % 10)));
	return bcdData;
}

uint8_t LT_BcdToAscii(uint8_t bcdData)
{
	uint8_t asciiData = 0;
	asciiData = (((bcdData & 0xf0) >> 4) * 10 + (bcdData & 0x0f));
	return asciiData;
}

void RTC_Set_time(tm timer)
{
	RTC->PRT1R = timer.day;
	RTC->PRT2R = timer.hour<<16 | timer.minute<<8 | timer.second;
//	LTPrintf("SetTime:Day1:%d Hour1:%d Min1:%d sec1:%d\r\n",timer.day,timer.hour,timer.minute,timer.second);
}

void RTC_Set_alarm(tm timer)
{
	RTC->PRA1R = (1<<15)|timer.day;
	RTC->PRA2R = (1<<23)|(1<<15)|(1<<7)|timer.hour<<16 | timer.minute<<8 | timer.second;
}
void RTC_GetTime(tm *t)
{
	uint32_t tim;


	tim = RTC->PRT1R;
	t->day = (uint16_t)(tim&(0x7FFF));

	tim = RTC->PRT2R;
	t->hour = (uint8_t)((tim>>16)&0x1f);
	t->minute = (uint8_t)((tim>>8)&0x3f);
	t->second = (uint8_t)((tim)&0x3f);


//	printf("t->day = %d\r\n", t->day);
//	printf("t->hour = %d\r\n", t->hour);
//	printf("t->minute = %d\r\n", t->minute);
//	printf("t->second = %d\r\n", t->second);
}
uint8_t LT_RtcInit(void)
{
	tm timer;
	tm timer_alarm;
	volatile  uint32_t timeout=3000;	// 3000ms
	uint32_t temp1,temp2,value,rtc_wclk_div;

	temp1=RTC->PRT1R;
	temp2=RTC->PRT2R;

	rtc_wclk_div=SYS_CLOCK/10000000;	//��Ƶֵ  SYS_CLOCK=168000000
	if (rtc_wclk_div >= 1)	rtc_wclk_div--;

	while(timeout--)
	{
		RTC->PRKEYR = 0x5aa55aa5;
		RTC->U32_PRCSR.Dir = 0;
		RTC->U32_PRCSR.Wclk_Div = rtc_wclk_div;

		RTC->PRT1R=0x5555;
		RTC->PRT2R=0x5555;
		value=RTC->PRT1R;
		if (value == 0x5555) break;
		DelayMS(1);
		WDT_FeedDog();
	}

	RTC->PRT1R=temp1;
	RTC->PRT2R=temp2;

	//RTC->U32_PRENR.RTC_EN = 0;		//disable RTC
	RTC->PRKEYR = 0x5aa55aa5;
	RTC->U32_PRCSR.Wclk_Div = rtc_wclk_div;	//��Ƶֵ
	RTC->U32_PRCSR.Dir = 0;
	RTC->U32_PRCSR.Sec_IEN = 1;		//�����ж�
 //	RTC->U32_PRCSR.Min_IEN = 1;		//�򿪷��ж�
 //	RTC->U32_PRCSR.Hou_IEN = 1;		//��ʱ�ж�
 //	RTC->U32_PRCSR.Day_IEN = 1;		//�����ж�
 //	RTC->U32_PRCSR.R_32KHz_IEN = 1;	//��32Khz�ж�
 //	RTC->U32_PRCSR.R_1KHz_IEN = 1;	//��1Khz�ж�
	RTC->U32_PRCSR.int_type = 1;	//������

	DelayMS(5);
//	LTPrintf("ReadTime:Day1:%d Hour1:%d Min1:%d sec1:%d\r\n",RTC->U32_PRT1R.DAYS,RTC->U32_PRT2R.HOU,RTC->U32_PRT2R.MIN,RTC->U32_PRT2R.SEC);

	timer.day = RTC_Get();
	if (timer.day == 0)
	{
		RTC_Set(2023, 10, 18, 16, 13, 10);
	}
	else
	{
		RTC_Set(calendar.w_year,calendar.w_month,calendar.w_date,calendar.hour,calendar.min,calendar.sec);
	}

	RTC->U32_PRCSR.Ala_IEN = 1;		//��alarm�ж�
	RTC->U32_PRENR.RTC_EN = 1;		//ʹ��RTC
	EIC->IER|=IRQ(13);

	return 0;
}

// RTC clock interrupt
void RTC_Handler(void)
{
	uint32_t prc1r;
	uint8_t i = 0, j = 0;
	uint8_t rebuf[40];

	u32 prcsr = RTC->PRCSR & 0xFF00FFFF;

	if((RTC->U32_PRCSR.Sec_IEN) && (RTC->U32_PRCSR.Sec_intf))
	{
		RTC->PRCSR = prcsr|(1<<19);

		RTC_Get();

		RTC_flag = 1;
		Clock_flag = 1;
		Count_flag = 1;
		for (i = 0; i < Count_sum; i++)
		{
			if (((var[gCount_Info[i].c_add * 2] << 8) + var[gCount_Info[i].c_add * 2 + 1]) == 1)
			{
				if (gCount_Info[i].dir == 0) // Count backwards
				{
					if (var[gCount_Info[i].t_add * 2 + 1] > 0)
							var[gCount_Info[i].t_add * 2 + 1] = var[gCount_Info[i].t_add * 2 + 1] - 1;
						
					else if (var[gCount_Info[i].t_add * 2 + 1] == 0 && var[gCount_Info[i].t_add * 2] > 0)
					{
						var[gCount_Info[i].t_add * 2] = var[gCount_Info[i].t_add * 2] - 1;
						var[gCount_Info[i].t_add * 2 + 1] = 0xFF;
					}
					else if (((var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1]) == 0)
					{
						var[gCount_Info[i].c_add * 2] = 0;
						var[gCount_Info[i].c_add * 2 + 1] = 0;
						
						for (j = 0; j < 8; j++)
						{
							if (gCount_Info[i].var_addr[j] != 0xFFFF && gCount_Info[i].var_addr[j] < VAR_ADDR_E)
							{
								var[gCount_Info[i].var_addr[j] * 2] = (gCount_Info[i].var[j] >> 8) & 0xFF;
								var[gCount_Info[i].var_addr[j] * 2 + 1] = gCount_Info[i].var[j] & 0xFF;

								if (gCount_Info[i].var_addr[j] >= VAR_ADDR_S)
								{
									//reg_operation(gCount_Info[i].var_addr[j]);
									Count_reg[Count_reg_num] = gCount_Info[i].var_addr[j];
									Count_reg_num++;
								}
							}
						}

						if (gCount_Info[i].end_feedback == 1) // Feedback
						{
							rebuf[3] = 0x41;
							for (j = 0; j < 8; j++)
							{
								rebuf[4 * j + 4] = gCount_Info[i].var_addr[j] >> 8;
								rebuf[4 * j + 5] = gCount_Info[i].var_addr[j] & 0xFF;
								rebuf[4 * j + 6] = gCount_Info[i].var[j] >> 8;
								rebuf[4 * j + 7] = gCount_Info[i].var[j] & 0xFF;
								Var_AddrToReg(&rebuf[4 * j + 4]);
							}
							LT_SendData_CRC_Frame(rebuf, 33);
						}
					}
				}
				else if (gCount_Info[i].dir == 1) // Positive number
				{
					if (((var[gCount_Info[i].t_add * 2] << 8) + var[gCount_Info[i].t_add * 2 + 1]) >=
						((var[gCount_Info[i].v_add * 2] << 8) + var[gCount_Info[i].v_add * 2 + 1]))
					{
						var[gCount_Info[i].c_add * 2] = 0;
						var[gCount_Info[i].c_add * 2 + 1] = 0;
						for (j = 0; j < 8; j++)
						{
							if (gCount_Info[i].var_addr[j] != 0xFFFF && gCount_Info[i].var_addr[j] < VAR_ADDR_E)
							{
								var[gCount_Info[i].var_addr[j] * 2] = (gCount_Info[i].var[j] >> 8) & 0xFF;
								var[gCount_Info[i].var_addr[j] * 2 + 1] = gCount_Info[i].var[j] & 0xFF;

								if (gCount_Info[i].var_addr[j] >= VAR_ADDR_S)
								{
									//reg_operation(gCount_Info[i].var_addr[j]);
									Count_reg[Count_reg_num] = gCount_Info[i].var_addr[j];
									Count_reg_num++;
									
								}
							}
						}

						if (gCount_Info[i].end_feedback == 1) // Feedback
						{
							rebuf[3] = 0x41;
							for (j = 0; j < 8; j++)
							{
								rebuf[4 * j + 4] = gCount_Info[i].var_addr[j] >> 8;
								rebuf[4 * j + 5] = gCount_Info[i].var_addr[j] & 0xFF;
								rebuf[4 * j + 6] = gCount_Info[i].var[j] >> 8;
								rebuf[4 * j + 7] = gCount_Info[i].var[j] & 0xFF;
								Var_AddrToReg(&rebuf[4 * j + 4]);
							}
							LT_SendData_CRC_Frame(rebuf, 33);
						}
					}

					if (((var[gCount_Info[i].c_add * 2] << 8) + var[gCount_Info[i].c_add * 2 + 1]) != 0x00)
					{
						if (var[gCount_Info[i].t_add * 2 + 1] < 0xFF)
							var[gCount_Info[i].t_add * 2 + 1] = var[gCount_Info[i].t_add * 2 + 1] + 1;
						else if (var[gCount_Info[i].t_add * 2 + 1] == 0xFF)
						{
							var[gCount_Info[i].t_add * 2] = var[gCount_Info[i].t_add * 2] + 1;
							var[gCount_Info[i].t_add * 2 + 1] = 0;
						}
					}
				}
			}
		}

		for (i = 0; i < Record_count; i++)
		{
			if (((var[gRecord_count_Info[i].c_add * 2] << 8) + var[gRecord_count_Info[i].c_add * 2 + 1]) == 1)
			{
				if (gRecord_count_Info[i].dir == 0) // Count backwards
				{
					if (var[gRecord_count_Info[i].t_add * 2 + 1] > 0)
						var[gRecord_count_Info[i].t_add * 2 + 1] = var[gRecord_count_Info[i].t_add * 2 + 1] - 1;
					else if (var[gRecord_count_Info[i].t_add * 2 + 1] == 0 && var[gRecord_count_Info[i].t_add * 2] > 0)
					{
						var[gRecord_count_Info[i].t_add * 2] = var[gRecord_count_Info[i].t_add * 2] - 1;
						var[gRecord_count_Info[i].t_add * 2 + 1] = 0xFF;
					}
					if (((var[gRecord_count_Info[i].t_add * 2] << 8) + var[gRecord_count_Info[i].t_add * 2 + 1]) == 0)
					{
						for (j = 0; j < 8; j++)
						{
							if (gRecord_count_Info[i].var_addr[j] != 0xfFFF && gRecord_count_Info[i].var_addr[j] < VAR_ADDR_E)
							{
								var[gRecord_count_Info[i].var_addr[j] * 2] = (gRecord_count_Info[i].var[j] >> 8) & 0xFF;
								var[gRecord_count_Info[i].var_addr[j] * 2 + 1] = gRecord_count_Info[i].var[j] & 0xFF;

								if (gRecord_count_Info[i].var_addr[j] >= VAR_ADDR_S)
								{
									//reg_operation(gRecord_count_Info[i].var_addr[j]);
									Count_reg[Count_reg_num] = gCount_Info[i].var_addr[j];
									Count_reg_num++;
								}
							}
						}

						if (gRecord_count_Info[i].end_feedback == 1) // Feedback
						{
							rebuf[3] = 0x41;
							for (j = 0; j < 8; j++)
							{
								rebuf[4 * j + 4] = gRecord_count_Info[i].var_addr[j] >> 8;
								rebuf[4 * j + 5] = gRecord_count_Info[i].var_addr[j] & 0xFF;
								rebuf[4 * j + 6] = gRecord_count_Info[i].var[j] >> 8;
								rebuf[4 * j + 7] = gRecord_count_Info[i].var[j] & 0xFF;
								Var_AddrToReg(&rebuf[4 * j + 4]);
							}
							LT_SendData_CRC_Frame(rebuf, 33);
						}
						gRecord_count_Info[i].v_add = gRecord_count_Info[Record_count - 1].v_add;
						gRecord_count_Info[i].t_add = gRecord_count_Info[Record_count - 1].t_add;
						gRecord_count_Info[i].c_add = gRecord_count_Info[Record_count - 1].c_add;
						gRecord_count_Info[i].dir = gRecord_count_Info[Record_count - 1].dir;
						gRecord_count_Info[i].end_feedback = gRecord_count_Info[Record_count - 1].end_feedback;
						gRecord_count_Info[i].var[0] = gRecord_count_Info[Record_count - 1].var[0];
						gRecord_count_Info[i].var_addr[0] = gRecord_count_Info[Record_count - 1].var_addr[0];
						gRecord_count_Info[i].var[1] = gRecord_count_Info[Record_count - 1].var[1];
						gRecord_count_Info[i].var_addr[1] = gRecord_count_Info[Record_count - 1].var_addr[1];
						gRecord_count_Info[i].var[2] = gRecord_count_Info[Record_count - 1].var[2];
						gRecord_count_Info[i].var_addr[2] = gRecord_count_Info[Record_count - 1].var_addr[2];
						gRecord_count_Info[i].var[3] = gRecord_count_Info[Record_count - 1].var[3];
						gRecord_count_Info[i].var_addr[3] = gRecord_count_Info[Record_count - 1].var_addr[3];
						gRecord_count_Info[i].var[4] = gRecord_count_Info[Record_count - 1].var[4];
						gRecord_count_Info[i].var_addr[4] = gRecord_count_Info[Record_count - 1].var_addr[4];
						gRecord_count_Info[i].var[5] = gRecord_count_Info[Record_count - 1].var[5];
						gRecord_count_Info[i].var_addr[5] = gRecord_count_Info[Record_count - 1].var_addr[5];
						gRecord_count_Info[i].var[6] = gRecord_count_Info[Record_count - 1].var[6];
						gRecord_count_Info[i].var_addr[6] = gRecord_count_Info[Record_count - 1].var_addr[6];
						gRecord_count_Info[i].var[7] = gRecord_count_Info[Record_count - 1].var[7];
						gRecord_count_Info[i].var_addr[7] = gRecord_count_Info[Record_count - 1].var_addr[7];
						i--;
						Record_count--;
					}
				}
				else if (gRecord_count_Info[i].dir == 1) // Positive number
				{
					if (var[gRecord_count_Info[i].t_add * 2 + 1] < 0xFF)
						var[gRecord_count_Info[i].t_add * 2 + 1] = var[gRecord_count_Info[i].t_add * 2 + 1] + 1;
					else if (var[gRecord_count_Info[i].t_add * 2 + 1] == 0xFF)
					{
						var[gRecord_count_Info[i].t_add * 2] = var[gRecord_count_Info[i].t_add * 2] + 1;
						var[gRecord_count_Info[i].t_add * 2 + 1] = 0;
					}

					if (((var[gRecord_count_Info[i].t_add * 2] << 8) + var[gRecord_count_Info[i].t_add * 2 + 1]) >=
						((var[gRecord_count_Info[i].v_add * 2] << 8) + var[gRecord_count_Info[i].v_add * 2 + 1]))
					{
						for (j = 0; j < 8; j++)
						{
							if (gRecord_count_Info[i].var_addr[j] != 0xFFFF && gRecord_count_Info[i].var_addr[j] < VAR_ADDR_E)
							{
								var[gRecord_count_Info[i].var_addr[j] * 2] = (gRecord_count_Info[i].var[j] >> 8) & 0xFF;
								var[gRecord_count_Info[i].var_addr[j] * 2 + 1] = gRecord_count_Info[i].var[j] & 0xFF;

								if (gRecord_count_Info[i].var_addr[j] >= VAR_ADDR_S)
								{
									//reg_operation(gRecord_count_Info[i].var_addr[j]);
									Count_reg[Count_reg_num] = gCount_Info[i].var_addr[j];
									Count_reg_num++;
								}
							}
						}

						if (gRecord_count_Info[i].end_feedback == 1) // Feedback
						{
							rebuf[3] = 0x41;
							for (j = 0; j < 8; j++)
							{
								rebuf[4 * j + 4] = gRecord_count_Info[i].var_addr[j] >> 8;
								rebuf[4 * j + 5] = gRecord_count_Info[i].var_addr[j] & 0xFF;
								rebuf[4 * j + 6] = gRecord_count_Info[i].var[j] >> 8;
								rebuf[4 * j + 7] = gRecord_count_Info[i].var[j] & 0xFF;
								Var_AddrToReg(&rebuf[4 * j + 4]);
							}
							LT_SendData_CRC_Frame(rebuf, 33);
						}

						gRecord_count_Info[i].v_add = gRecord_count_Info[Record_count - 1].v_add;
						gRecord_count_Info[i].t_add = gRecord_count_Info[Record_count - 1].t_add;
						gRecord_count_Info[i].c_add = gRecord_count_Info[Record_count - 1].c_add;
						gRecord_count_Info[i].dir = gRecord_count_Info[Record_count - 1].dir;
						gRecord_count_Info[i].end_feedback = gRecord_count_Info[Record_count - 1].end_feedback;
						gRecord_count_Info[i].var[0] = gRecord_count_Info[Record_count - 1].var[0];
						gRecord_count_Info[i].var_addr[0] = gRecord_count_Info[Record_count - 1].var_addr[0];
						gRecord_count_Info[i].var[1] = gRecord_count_Info[Record_count - 1].var[1];
						gRecord_count_Info[i].var_addr[1] = gRecord_count_Info[Record_count - 1].var_addr[1];
						gRecord_count_Info[i].var[2] = gRecord_count_Info[Record_count - 1].var[2];
						gRecord_count_Info[i].var_addr[2] = gRecord_count_Info[Record_count - 1].var_addr[2];
						gRecord_count_Info[i].var[3] = gRecord_count_Info[Record_count - 1].var[3];
						gRecord_count_Info[i].var_addr[3] = gRecord_count_Info[Record_count - 1].var_addr[3];
						gRecord_count_Info[i].var[4] = gRecord_count_Info[Record_count - 1].var[4];
						gRecord_count_Info[i].var_addr[4] = gRecord_count_Info[Record_count - 1].var_addr[4];
						gRecord_count_Info[i].var[5] = gRecord_count_Info[Record_count - 1].var[5];
						gRecord_count_Info[i].var_addr[5] = gRecord_count_Info[Record_count - 1].var_addr[5];
						gRecord_count_Info[i].var[6] = gRecord_count_Info[Record_count - 1].var[6];
						gRecord_count_Info[i].var_addr[6] = gRecord_count_Info[Record_count - 1].var_addr[6];
						gRecord_count_Info[i].var[7] = gRecord_count_Info[Record_count - 1].var[7];
						gRecord_count_Info[i].var_addr[7] = gRecord_count_Info[Record_count - 1].var_addr[7];
						i--;
						Record_count--;
					}
				}
			}
		}
	}

	if((RTC->U32_PRCSR.Min_IEN) && (RTC->U32_PRCSR.Min_intf))
	{
		RTC->PRCSR = prcsr|(1<<20);
//		flag_min = 1;
//		LTPrintf("Min_Int\r\n");
	}
	if((RTC->U32_PRCSR.Hou_IEN) && (RTC->U32_PRCSR.Hou_intf))
	{
		RTC->PRCSR = prcsr|(1<<21);
//		flag_hou = 1;
//		LTPrintf("Hou_Int\r\n");
	}
	if((RTC->U32_PRCSR.Day_IEN) && (RTC->U32_PRCSR.Day_intf))
	{
		RTC->PRCSR = prcsr|(1<<22);
//		flag_day = 1;
//		LTPrintf("Day_Int\r\n");
	}
	if((RTC->U32_PRCSR.R_32KHz_IEN) && (RTC->U32_PRCSR.R_32KHz_intf))
	{
		RTC->PRCSR |= (1<<16);

//		if(test_val%2==0)		EPORT0->EPDR &=~0x10;
//		else if(test_val%2==1)	EPORT0->EPDR |= 0x10; ;
//		test_val++;
//		LTPrintf("32Khz_Int\r\n");
	}
	if((RTC->U32_PRCSR.R_1KHz_IEN) && (RTC->U32_PRCSR.R_1KHz_intf))
	{
		RTC->PRCSR = prcsr|(1<<17);

//		if(test_val%2==0)		EPORT0->EPDR &=~0x10;
//		else if(test_val%2==1)	EPORT0->EPDR |= 0x10; ;
//		test_val++;
//		LTPrintf("32Khz_Int\r\n");
	}

	if((RTC->U32_PRCSR.Ala_IEN) && (RTC->U32_PRCSR.Ala_intf))
	{
		RTC->PRCSR = prcsr|(1<<18);

//		flag_alarm = 1;
//		LTPrintf("ala_Int\r\n");
//		LTPrintf("Set:Day1:%d Hour1:%d Min1:%d sec1:%d\r\n",RTC->U32PRT1R.DAYS,RTC->U32PRT2R.HOU,RTC->U32PRT2R.MIN,RTC->U32PRT2R.SEC);
//		LTPrintf("Read:Day2:%d Hour2:%d Min2:%d sec2:%d\r\n",RTC->U32PRTCR.DAYS,RTC->U32PRTCR.HOU,RTC->U32PRTCR.MIN,RTC->U32PRTCR.SEC);
	}
}


// Monthly data table
uint8_t const table_week[12] = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5}; // Monthly correction data sheet
// Date table of the month in the ordinary year
const uint8_t mon_table[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

// month           1  2  3  4  5  6  7  8  9  10 11 12
// leap year       31 29 31 30 31 30 31 31 30 31 30 31
// non-leap years  31 28 31 30 31 30 31 31 30 31 30 31
/********************************************************************************
* Function Name	: Is_Leap_Year
* Description  	: Judge whether it is a leap year.
* Input        	: - year: 
* Output       	: None
* Return       	: Is this year a leap year. 1:yes, 0:no
*********************************************************************************/
uint8_t Is_Leap_Year(uint16_t year)
{
	if(((year%4==0)&&(year%100!=0))||(year%400==0))return 1;
	return 0;
}

/********************************************************************************
* Function Name	: RTC_Get_Week
* Description  	: Get the week according to the gregorian calendar date.
* Input        	: - year: Only gregorian 1901-2099 allowed
                  - month: Gregorian month
                  - day: Gregorian day
* Output       	: None
* Return       	: week
*********************************************************************************/
uint8_t RTC_Get_Week(uint16_t year, uint8_t month, uint8_t day)
{
	uint16_t temp2;
	uint8_t yearH, yearL;

	yearH = year / 100;
	yearL = year % 100;
	// If it is the 21st century, add 100 years
	if (yearH > 19)
		yearL += 100;
	// The number of leap years passed is only counted after 1900
	temp2 = yearL + yearL / 4;
	temp2 = temp2 % 7;
	temp2 = temp2 + day + table_week[month - 1];
	if (yearL % 4 == 0 && month < 3)
		temp2--;
	return (temp2 % 7);
}

uint16_t RTC_DateToDays(uint16_t syear, uint8_t smon, uint8_t sday)
{
	uint16_t t;
	uint16_t days = sday;

	if(syear<RTC_BASE_YEAR) return 0;

	for (t = RTC_BASE_YEAR; t < syear; t++) // Add the seconds of all years
	{
		if (Is_Leap_Year(t))
			days += 366; // Days in leap year
		else
			days += 365; // Days in non-leap years
	}

	for (t = 0; t < smon -1; t++) // Add the number of seconds in the previous month
	{
		days += (uint32_t)mon_table[t]; // Add months and seconds
		if (Is_Leap_Year(syear) && t == 1)
			days++; // Increase the number of seconds of one day in February of leap year
	}

	//days += (sday - 1);
	return days;
}

/********************************************************************************
* Function Name	: RTC_Set
* Description  	: set clock.
* Input        	: - syear: Only gregorian 1970-2099 allowed
                  - smon:
                  - sday:
                  - hour:
                  - min:
                  - sec:
* Output       	: None
* Return       	: week
*********************************************************************************/
uint8_t RTC_Set(uint16_t syear, uint8_t smon, uint8_t sday, uint8_t hour, uint8_t min, uint8_t sec)
{
	tm timer;
	uint16_t temp;

	if (hour >= 24) hour = 23;
	if (min >= 60) min = 59;
	if (sec >= 60) sec = 59;
	timer.hour = hour;
	timer.minute = min;
	timer.second = sec;

	if(syear == 0 || smon == 0 || sday == 0)
		return 0; // error

	if(smon == 2)
	{
		if(sday>mon_table[smon-1]+Is_Leap_Year(syear))
			return 0; // error
	}
	else
	{
		if(sday>mon_table[smon-1])
			return 0; // error
	}

	timer.day = RTC_DateToDays(syear,smon,sday);
	RTC_Set_time(timer);

	return 0;
}

/********************************************************************************
* Function Name	: RTC_Get
* Description  	: Get calendar date and time.
* Input        	: - year: Only gregorian 1970-2099 allowed
                  - month: Gregorian month
                  - day: Gregorian day
* Output       	: None
* Return       	: week
*********************************************************************************/
uint16_t RTC_Get(void)
{
	static uint16_t daycnt = 0;
	uint16_t temp, days;
	uint8_t monday[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	uint16_t year, mon, day;
	tm timer;

	RTC_GetTime(&timer);
	days = timer.day;		// Get Days


	if (daycnt != days) // More than a day
	{

		daycnt = days;
		year = RTC_BASE_YEAR; // From base_year

		// Calculate year
		while (days > 365)
		{
			if (Is_Leap_Year(year)) // leap year
				temp = 366;
			else
				temp = 365;
			if(days<=temp) break;
			days -= temp;
			year++;
		}

		 // Calculate month and day
		if(Is_Leap_Year(year)) monday[1] = 29;
		for (temp = 0; temp < 12; temp++) {
       		if (days <= monday[temp])
            	break;
        	days -= monday[temp];
  	  	}
		mon = temp + 1;
		day = days;

		calendar.w_year = year; // Get year
		calendar.w_month = mon ; // Get month
		calendar.w_date = day;	  // Get date
//		LTPrintf("RTC_Get year.month.day %d.%d.%d \r\n", calendar.w_year,calendar.w_month,calendar.w_date);
	}
	calendar.hour = timer.hour;
	calendar.min = timer.minute;
	calendar.sec = timer.second;
	calendar.week = RTC_Get_Week(calendar.w_year, calendar.w_month, calendar.w_date); // Get week


//	var[(VAR_TIME + 0) * 2 + 1] = calendar.w_year - 2000;
//	var[(VAR_TIME + 1) * 2 + 1] = calendar.w_month;
//	var[(VAR_TIME + 2) * 2 + 1] = calendar.w_date;
//	var[(VAR_TIME + 3) * 2 + 1] = calendar.hour;
//	var[(VAR_TIME + 4) * 2 + 1] = calendar.min;
//	var[(VAR_TIME + 5) * 2 + 1] = calendar.sec;
//	var[VAR_TIMESET * 2 + 1] = calendar.week;
	
	return timer.day;
}


